package com.nurt.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.nurt.dao.OrderDao;
import com.nurt.dao.DishDao;
import com.nurt.dao.OrderDishDao;
import com.nurt.dao.impl.DishDaoImpl;
import com.nurt.dao.impl.OrderDaoImpl;
import com.nurt.dao.impl.OrderDishDaoImpl;
import com.nurt.entity.DishEntity;
import com.nurt.entity.OrderEntity;
import com.nurt.entity.OrderDishEntity;
import com.nurt.service.OrderService;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao;
    private DishDao dishDao;
    private OrderDishDao orderDishDao;

    public OrderServiceImpl() {
        this.orderDao = new OrderDaoImpl();
        this.dishDao = new DishDaoImpl();
        this.orderDishDao = new OrderDishDaoImpl();
    }

    public List<OrderEntity> getOrders(int userId) {
        List<OrderEntity> orders = new ArrayList<>();
        try {
            orders = orderDao.selectOrders(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    public List<OrderEntity> getOrdersByStatus(int userId, int status) {
        List<OrderEntity> orders = null;
        try {
            orders = orderDao.selectOrdersByStatus(userId, status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    public int createOrder(OrderEntity order) {
        try {
            int orderId = orderDao.insertOrder(order);
            return orderId;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public void updateOrderStatus(int orderId, int status) {
        try {
            orderDao.updateOrderStatus(orderId, status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int updateCart(int orderId, int dishId, int quantity) {
        try {
            List<OrderDishEntity> orderDishes = orderDishDao.selectOrderDishes(orderId);

            for (OrderDishEntity orderDish : orderDishes) {
                if (orderDish.getDishId() == dishId) {
                    orderDish.setQuantity(quantity);
                    return orderDishDao.updateOrderDish(orderDish);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }

        return -1;
    }

    public int addToCart(int orderId, int dishId) {

        try {
            List<OrderDishEntity> orderDishes = orderDishDao.selectOrderDishes(orderId);

            for (OrderDishEntity orderDish : orderDishes) {
                if (orderDish.getDishId() == dishId) {
                    // Update the quantity of the existing order dish
                    orderDish.increQuantity(1);
                    orderDishDao.updateOrderDish(orderDish);
                    return orderDish.getOrderDishId();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
                
        try {
            OrderDishEntity newOrderDish = new OrderDishEntity();
            newOrderDish.setOrderId(orderId);
            newOrderDish.setDishId(dishId);

            DishEntity dish = dishDao.selectDish(dishId);
            newOrderDish.setName(dish.getName());
            newOrderDish.setQuantity(1);
            newOrderDish.setPrice(dish.getPrice());

            int orderDishId = orderDishDao.insertOrderDish(newOrderDish);
            return orderDishId;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public void removeFromCart(int orderId, int dishId) {
        try {
            List<OrderDishEntity> orderDishes = orderDishDao.selectOrderDishes(orderId);
            for (OrderDishEntity orderDish : orderDishes) {
                if (orderDish.getDishId() == dishId) {
                    orderDishDao.deleteOrderDish(orderDish.getOrderDishId());
                    break;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<OrderDishEntity> getCartContents(int orderId) {
        try {
            List<OrderDishEntity> orderDishes = orderDishDao.selectOrderDishes(orderId); 
            return orderDishes;
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();  // 返回一个空列表
        }
    }

    public void clearCart(int orderId) {
        try {
            List<OrderDishEntity> orderDishes = orderDishDao.selectOrderDishes(orderId);
            for (OrderDishEntity orderDish : orderDishes) {
                orderDishDao.updateOrderDish(orderDish);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void placeOrder(OrderEntity order) {
        try {
            orderDao.insertOrder(order);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}